package com.example.farmer.Fragment.Message;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.farmer.Database.ChatMessageDatabase;
import com.example.farmer.Entity.ChatMessage;
import com.example.farmer.R;

import java.util.List;

public class MessageFragment extends Fragment {

    private RecyclerView recyclerView;
    private InboxMessageAdapter adapter;
    private ChatMessageDatabase database;

    public MessageFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_xiaoxi, container, false);
        recyclerView = view.findViewById(R.id.recyclerViewMessages);

        // 初始化数据库帮助类
        database = new ChatMessageDatabase(getContext());

        // 获取消息列表，这里假设productId已知
        int productId = 1; // 示例productId
        List<ChatMessage> messages = database.loadChatMessagesFromDatabase(productId);
        adapter = new InboxMessageAdapter(messages,getContext());

        // 设置适配器和布局管理器
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 关闭数据库连接
        if (database != null) {
            database.close();
        }
    }
}